/**
 * 
 */
package com.java.controller.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.java.entity.Student;
import com.java.entity.Teacher;
import com.java.entity.Users;
import com.java.service.impl.StudentServiceImpl;
import com.java.service.impl.TeacherSeriveImpl;
import com.java.service.impl.UserServiceImpl;

/**
* @author Kidney
* 创建时间：2019年4月29日 下午2:00:52
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class UserController {

	@Autowired
	private UserServiceImpl us;

	@Autowired
	private TeacherSeriveImpl ts;
	
	
	@Autowired
	private  StudentServiceImpl ss;

	@RequestMapping("change_password")

	public String changePass(String password, HttpServletRequest request, Model model) {

		Subject subject = SecurityUtils.getSubject();

		Users users = (Users) subject.getPrincipal();

		String salt = "siyuan";
		int times = 1;
		Md5Hash md = new Md5Hash(password, salt, times);

		users.setPassword(md.toString());
		int flag = us.updatePassword(users);

		// String jsonString = JSON.toJSONString(flag);

		if (flag == 1) {
			request.setAttribute("msg", "修改成功");
			return "pages/user/changepass.jsp";
		} else
			request.setAttribute("msg", "修改失败");
		return "pages/user/changepass.jsp";
	}

	@RequestMapping("pass_home")
	public String password() {

		return "pages/user/changepass.jsp";
	}

	@RequestMapping(value = "teacher_details", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String teacherDetails() {

		Teacher teacher = new Teacher();
		
		Subject subject = SecurityUtils.getSubject();
		Users users = (Users) subject.getPrincipal();
		System.out.println(users);
		teacher = ts.getTeacher(Integer.valueOf(users.getUsername()));

		String jsonString = JSON.toJSONString(teacher);
		return jsonString;
	}
	
	@RequestMapping(value = "student_details", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String studentDetails() {

		
		
		Subject subject = SecurityUtils.getSubject();
		Users users = (Users) subject.getPrincipal();
		
		System.out.println(users);
		
		
		Student student  = ss.getStudent(Integer.valueOf(users.getUsername()));

		String jsonString = JSON.toJSONString(student);
		return jsonString;
	}
	
	
	
	

}
